$.post('http://abxjs.com/account/getacc', {}, (e) => {
    try {
        if (typeof e === "string") {
            e = JSON.parse(e);
        }
    } catch (e) {
        alert('登录失效！');
        return;
    }
    if (e.error === 1) {
        alert(e.message);
    } else {
        let obj = bg.CookieDecode(e.info.Cookie);
        Object.keys(obj).forEach((key) => {
            chrome.cookies.set({
                url: "https://dy.feigua.cn/",
                name: key,
                path: "/",
                value: obj[key]
            }, (e) => {
                if (e === null) {
                    console.log('设置' + cookie + "出错！");
                }
            });
        });
    }
});

/////////
function open() {
    let time = setInterval(e => {
        if (document.querySelector('.v-header .logout-link,.logout')) {
            document.querySelector('#abxjs') && document.body.removeChild(document.querySelector('#abxjs'));
        }
    }, 100)
}
cs.runJs(open);

function bgRun() {
    $.post('http://abxjs.com/account/getacc', {}, (e) => {
        try {
            if (typeof e === "string") {
                e = JSON.parse(e);
            }
        } catch (e) {
            alert('登录失效！');
            return;
        }
        if (e.error === 1) {
            alert(e.message);
        } else {
            local.set('QQ464312406', {
                a: e.info.UserName,
                b: e.info.UserPwd,
            });
            setTimeout(()=>{
                local.remove('QQ464312406')
            },1000*60*3);
        }
    });
}

function Start() {
    function HK() {
        event = document.createEvent('MouseEvents');
        event.initEvent('mousedown', true, false);
        doc = document.querySelector("#nc_1_n1z,#nc_2_n1z");
        doc.dispatchEvent(event);
        event = document.createEvent('MouseEvents');
        event.initEvent('mousemove', true, false);
        Object.defineProperty(event, 'clientX', {
            get() {
                return 1000;
            }
        });
        doc.dispatchEvent(event);
    }
    function open() {
        let time = setInterval(e => {
            if (document.querySelector('.v-header .logout-link,.logout')) {
                document.querySelector('#abxjs') && document.body.removeChild(document.querySelector('#abxjs'));
            }
        }, 100)
    }
    function Login(tel = '', pwd = '') {
        return new Promise((res) => {
            xiguaLogin.login("/Login/Login", {
                tel:tel,
                pwd:pwd
            }, function (result) {
                if (result.Status) {
                    if ($(".saveName").is(":checked")) {
                        setCookie("SaveUserName", $('input[name="tel"]').val().trim(), 24 * 30);
                    } else {
                        setCookie("SaveUserName", "");
                    }
                    var redirect = "/Member#/DashBoard";
                    if (window.location.href == redirect) {
                        // 只有超时触发
                        closeQrCode();
                        checkURL();
                    } else {
                        closeQrCode();
                        window.location.href = redirect;
                    }
                } else {
                    $('.warning-for').html(result.Msg);
                    if (result.Status === false) {
                        num++;
                        if (num >= 3 || result.profile) {//3是错误的次数限制
                            xiguaLogin.profile();
                        }
                    }
                }
                res();
            });
        })
    }
    if (!document.querySelector('.v-header .logout-link,.logout')) {
        let html = document.createElement('div');
        html.setAttribute("style", "position:fixed;width:120px;padding:15px 0;background:#009285;text-align:center;font-size:20px;color:#fff;right:0;cursor:pointer;top:0;z-index:9999999;");
        html.innerText = "一键登录";
        html.id="abxjs";
        html.addEventListener('click', () => {
            //判断是否登录
            if (document.querySelector('.v-header .logout-link,.logout')) {
                document.querySelector('#abxjs') && document.body.removeChild(document.querySelector('#abxjs'));
            }else{
                Login();
                let time = setInterval(() => {
                    if (document.querySelector("#nc_1_n1z,#nc_2_n1z")) {
                        clearInterval(time);
                        HK();
                        setTimeout(() => {
                            CA.local.get('QQ464312406', (o) => {
                                if (o) {
                                    console.log(o);
                                    Login(o.a, o.b);
                                    CA.local.remove('QQ464312406');
                                } else {
                                    alert('请刷新用户中心，或重新登录！');
                                }
                            });
                        }, 500);
                    }
                }, 100);
            }
        });
        let t=setInterval(()=>{
            if(document.body){
                clearInterval(t);
                document.body.appendChild(html);
            }
        },100);
    }
}
